package com.glodon.paas.account.api.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.codehaus.jackson.annotate.JsonIgnore;

public class Org {
    private String id;
    private String owner;
    private String rootId;
    private String parentId;
    private String name;
    private int seq = -1;
    private List<Member> members;
    private List<Org> subOrgs;
    @JsonIgnore
    private Date createTime;
    @JsonIgnore
    private Date updateTime;

    public Org() {
    }

    public Org(String user, String name) {
        owner = user;
        this.name = name;
    }

    public Org(String user, String parentId, String name) {
        this.parentId = parentId;
        owner = user;
        this.name = name;
    }

    public Org(String parentId) {
        this.parentId = parentId;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public String getRootId() {
        return rootId;
    }

    public void setRootId(String rootId) {
        this.rootId = rootId;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSeq() {
        return seq;
    }

    public void setSeq(int seq) {
        this.seq = seq;
    }

    public List<Member> getMembers() {
        return members;
    }

    public void setMembers(List<Member> members) {
        this.members = members;
    }

    public List<Org> getSubOrgs() {
        return subOrgs;
    }

    public void setSubOrgs(List<Org> subOrgs) {
        this.subOrgs = subOrgs;
    }

    public void addSubOrg(Org subOrg) {
        if (subOrgs == null)
            subOrgs = new ArrayList<Org>();
        subOrgs.add(subOrg);
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public boolean sibling(Org org) {
        if (parentId == null) {
            return org.getParentId() == null;
        } else {
            return parentId.equals(org.getParentId());
        }
    }

    @Override
    public int hashCode() {
        return id.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        return id.equals(((Org) obj).id);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
